package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.SysUser;
import org.example.service.SysUserService;
import org.example.vo.MySecurityUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author：无敌暴龙战士
 * @Package：org.example.service.impl
 * @Project：learn_spring_security
 * @name：SecurityUserDetailsServiceImpl
 * @Date：2025/2/13 3:48
 * @Filename：SecurityUserDetailsServiceImpl
 * @Description:
 */
@Service
@Slf4j
public class SecurityUserDetailsServiceImpl implements UserDetailsService {


    @Resource
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUserName(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("user is not found");
        }

        MySecurityUser mySecurityUser = new MySecurityUser(sysUser);
        return mySecurityUser;
    }
}
